<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * App\Model\Member
 *
 * @property-read \App\Model\Cards $card
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member query()
 * @property int $id
 * @property int|null $type 用户类型:0管理员1城市合伙人2运营3业务4会员
 * @property int $pid 上级id，只允许城市id和会员id
 * @property int $salesman_id 业务员id
 * @property int $operator_id 运营商id
 * @property int $place_partner_id 城市合伙人id
 * @property int|null $enterprise_user_id 关联企业id，用于企业返佣
 * @property int $is_fase_distribution 是否是虚拟账号，1是，0不是
 * @property int $is_change_grade 是否由业务员变为会员,0可以改变，1不能改变
 * @property string|null $account 账号
 * @property string $phone 电话号码
 * @property string|null $password 密码
 * @property string|null $email email
 * @property float|null $money 总金额
 * @property float $maibao 脉宝
 * @property float $maibao_real 没有通过经销商的脉宝
 * @property float $maibao_all 累计脉宝
 * @property float $custom_income 个人网页收入
 * @property string|null $payment_password 支付密码
 * @property float|null $availableAmount 可提金额
 * @property float|null $freezingAmount 冻结金额
 * @property int|null $is_enabled 是否启用 0启用 1禁用
 * @property string|null $enabled_at 封停开启时间
 * @property string $logged_ip ip
 * @property string|null $logged_at 最后登录时间
 * @property float $agency_fee 代理费
 * @property float|null $currentCommission 当前可以佣金
 * @property float $rewards_all 奖励金总计(只增不减)
 * @property float|null $changeCommission 累计转余额的佣金
 * @property string|null $qr_code 用户生成二维码地址
 * @property int|null $from_type 通过活动进入类型1普通活动， 2特殊活动
 * @property string|null $invitation_code 邀请码
 * @property \Illuminate\Support\Carbon|null $created_at 创建时间
 * @property \Illuminate\Support\Carbon|null $updated_at 修改时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereAccount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereAgencyFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereAvailableAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereChangeCommission($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereCurrentCommission($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereCustomIncome($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereEnabledAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereEnterpriseUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereFreezingAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereFromType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereInvitationCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereIsChangeGrade($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereIsEnabled($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereIsFaseDistribution($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereLoggedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereLoggedIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereMaibao($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereMaibaoAll($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereMaibaoReal($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereOperatorId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member wherePaymentPassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member wherePlacePartnerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereQrCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereRewardsAll($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereSalesmanId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereUpdatedAt($value)
 * @property float|null $current_commission 会员当前可以佣金
 * @property float|null $change_commission 会员累计转余额的佣金
 * @property float|null $salesman_current_commission 业务员当前可以佣金
 * @property float|null $salesman_change_commission 业务员累计转余额的佣金
 * @property float $salesman_rewards_all 业务员奖励金总计(只增不减)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereSalesmanChangeCommission($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereSalesmanCurrentCommission($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereSalesmanRewardsAll($value)
 * @property string|null $register_source wechat 公众号， app。注册来源
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Member whereRegisterSource($value)
 */
class Member extends Model
{
    protected $table = 'users';

    public function card()
    {
        return $this->hasOne(Cards::class, 'user_id');
    }

//    public function level()
//    {
//        return $this->hasOne(VipFeeConfig::class);
//    }

    /**
     * 获取列表
     * @return array
     */
    public function lists()
    {
        $user = request()->user();
        $m = DB::table('users')->select('cards.user_name','cards.province','cards.company','cards.profession','cards.industry', 'users.id', 'users.phone','users.created_at','users.logged_at')
            ->leftJoin('cards', 'users.id', '=', 'cards.user_id');
        switch ($user->type) {
            case 1:
                $m->where("users.place_partner_id", $user->id);
                break;
            case 2:
                $m->where("users.operator_id", $user->id);
                break;
            case 3:
                $m ->where("users.salesman_id", $user->id);
                break;
        }
        if (request('type')) {
            $m->where('users.is_fase_distribution', '<>', 1)
                ->where('users.is_change_grade', '<>', 1);
        }

//        $m = DB::table('users')->leftJoin('cards', 'users.id', '=', 'cards.user_id')->where('type', '=', 4);

        if (request()->has('name')) {
            $m->where('cards.user_name', 'like', '%' . request('name') . '%');
        }
        if (request()->has('phone')) {
            $m->where('users.phone', '=', request('phone'));
        }
        if (request()->has('province')) {

        }
        if (request()->has('reg_time_start')) {
            $m->where('users.created_at', '>=', request('reg_time_start'));
        }
        if (request()->has('reg_time_end')) {
            $m->where('users.created_at', '<=', request('reg_time_end'));
        }
        if (request()->has('reg_time_start') and request()->has('reg_time_end')) {
            $m->whereBetween('users.created_at', [request('reg_time_start'), request('reg_time_end')]);
        }

        if (request()->has('last_login_start')) {
            $m->where('users.logged_at', '>=', request('last_login_start'));
        }
        if (request()->has('last_login_end')) {
            $m->where('users.logged_at', '<=', request('last_login_end'));
        }

        if (request()->has('last_login_start') and request()->has('last_login_end')) {
            $m->whereBetween('users.logged_at', [request('last_login_start'), request('last_login_end')]);
        }
        $limit = request('limit', 15);
        $result = $m->where('type',4)->paginate($limit);
        $data = $result->toArray();
        $data['data'] = [];
        foreach ($result as $item) {
            $data['data'][] = [
                'id' => $item->id,
                'name' => $item->user_name,
                'phone' => $item->phone,
                'level' => 0,
                'vip_expire' => '2018-12-23',
                'company' => $item->company,
                'profession' => $item->profession,
                'industry' => $item->industry,
                'created_at' => $item->created_at,
                'last_log_time' => $item->logged_at
            ];

        }
        return $data;
    }
}
